package org.ruoyi.aibox.service.impl;

import org.ruoyi.common.core.utils.MapstructUtils;
import org.ruoyi.core.page.TableDataInfo;
import org.ruoyi.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.ruoyi.aibox.domain.bo.DocAuditResultBo;
import org.ruoyi.aibox.domain.vo.DocAuditResultVo;
import org.ruoyi.aibox.domain.DocAuditResult;
import org.ruoyi.aibox.mapper.DocAuditResultMapper;
import org.ruoyi.aibox.service.IDocAuditResultService;
import org.ruoyi.common.core.utils.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 文档审核结果Service业务层处理
 *
 * @author xpeter
 * @date 2025-04-29
 */
@RequiredArgsConstructor
@Service
public class DocAuditResultServiceImpl implements IDocAuditResultService {

    private final DocAuditResultMapper baseMapper;

    /**
     * 查询文档审核结果
     */
    @Override
    public DocAuditResultVo queryById(Long resultId){
        return baseMapper.selectVoById(resultId);
    }

    /**
     * 查询文档审核结果列表
     */
    @Override
    public TableDataInfo<DocAuditResultVo> queryPageList(DocAuditResultBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DocAuditResult> lqw = buildQueryWrapper(bo);
        Page<DocAuditResultVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询文档审核结果列表
     */
    @Override
    public List<DocAuditResultVo> queryList(DocAuditResultBo bo) {
        LambdaQueryWrapper<DocAuditResult> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DocAuditResult> buildQueryWrapper(DocAuditResultBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DocAuditResult> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTaskId() != null, DocAuditResult::getTaskId, bo.getTaskId());
        lqw.eq(StringUtils.isNotBlank(bo.getDocId()), DocAuditResult::getDocId, bo.getDocId());
        lqw.like(StringUtils.isNotBlank(bo.getDocName()), DocAuditResult::getDocName, bo.getDocName());
        lqw.eq(StringUtils.isNotBlank(bo.getDocType()), DocAuditResult::getDocType, bo.getDocType());
        lqw.like(StringUtils.isNotBlank(bo.getAuditObjectName()), DocAuditResult::getAuditObjectName, bo.getAuditObjectName());
        lqw.like(StringUtils.isNotBlank(bo.getModelName()), DocAuditResult::getModelName, bo.getModelName());
        lqw.eq(StringUtils.isNotBlank(bo.getAuditStatus()), DocAuditResult::getAuditStatus, bo.getAuditStatus());
        lqw.eq(bo.getTotalItems() != null, DocAuditResult::getTotalItems, bo.getTotalItems());
        lqw.eq(bo.getAuditPassItems() != null, DocAuditResult::getAuditPassItems, bo.getAuditPassItems());
        lqw.eq(bo.getAuditedItems() != null, DocAuditResult::getAuditedItems, bo.getAuditedItems());
        lqw.eq(bo.getAuditScore() != null, DocAuditResult::getAuditScore, bo.getAuditScore());
        lqw.eq(StringUtils.isNotBlank(bo.getAuditAiConclusion()), DocAuditResult::getAuditAiConclusion, bo.getAuditAiConclusion());
        lqw.eq(bo.getAuditTime() != null, DocAuditResult::getAuditTime, bo.getAuditTime());
        return lqw;
    }

    /**
     * 新增文档审核结果
     */
    @Override
    public Boolean insertByBo(DocAuditResultBo bo) {
        DocAuditResult add = MapstructUtils.convert(bo, DocAuditResult.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setResultId(add.getResultId());
        }
        return flag;
    }

    /**
     * 修改文档审核结果
     */
    @Override
    public Boolean updateByBo(DocAuditResultBo bo) {
        DocAuditResult update = MapstructUtils.convert(bo, DocAuditResult.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DocAuditResult entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除文档审核结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
